package com.ljp.chat.execute;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.ljp.chat.entity.pojo.BaseEntity;
import com.ljp.chat.support.enhancement.RestSupportAdapter;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * <p>
 * 代码自动生成器
 * </p>
 *
 * @author LvJunPeng
 * @since 2022-11-02
 */
public class GeneratorExecute {

    public static void main(String[] args) {
        //修改配置即用
        new GeneratorFactory()
                .setDataBaseUrl("jdbc:mysql://121.41.46.175:3306/chat")
                .setDataBaseUserName("root")
                .setDataBasePassword("LJP121016")
                .setAuthor("LvJunPeng")
                .setOutPutAddress("/Users/lvjunpeng/LianTu/Projects/Java/chat/src/main/java/com/ljp/chat/temp")
                .setPackAgeName("com.ljp.chat")
                .setIncludeTables(new String[]{
                        "user", "account"
                })
                .setIgnoreColumns(new String[]{
                        "created_at", "updated_at"
                })
                .execute();
    }

}

@Data
@Accessors(chain = true)
class GeneratorFactory {
    private String dataBaseUrl;
    private String dataBaseUserName;
    private String dataBasePassword;
    private String author;
    private String outPutAddress;

    private String packAgeName;
    private String[] includeTables;
    private String[] ignoreColumns;

    public void execute() {
        FastAutoGenerator.create(this.dataBaseUrl, this.dataBaseUserName, this.dataBasePassword)
                .globalConfig(builder -> {
                    builder.author(this.author) // 设置作者
                            .outputDir(this.outPutAddress) // 指定输出目录
                            .disableOpenDir(); //禁止打开文件系统
                })
                .packageConfig(builder -> {
                    builder.parent(this.packAgeName); // 设置父包名
                })
                .strategyConfig(builder -> {
                    builder.addInclude(this.includeTables)//设置需要生成的表名
                            .entityBuilder().enableChainModel().enableLombok().addIgnoreColumns(this.ignoreColumns).superClass(BaseEntity.class).disableSerialVersionUID()//设置实体类
                            .controllerBuilder().enableRestStyle().superClass(RestSupportAdapter.class)//设置controller
                            .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl");//设置service
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}



